@extends('layouts.application')

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">物流管理</strong> /
                    <small>Express List</small>
                </div>
            </div>

            <br>
            @include('layouts.shared._flash')

            <div class="am-g">
                <div class="am-u-sm-12">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('shop.express.create')}}" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="delete_all am-btn am-btn-default"><span
                                        class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <hr>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check">
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>编号</th>
                                <th>物流名称</th>
                                <th>配送方式描述</th>
                                <th>运费/满额包邮</th>
                                <th>是否可用</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($expresses as $express)
                                <tr data-id="{{$express->id}}">
                                    <td>
                                        <input type="checkbox" name="check_id[]" value="{{$express->id}}"
                                               class="checked_id"/>
                                    </td>
                                    <td>{{$express->id}}</td>
                                    <td>{{$express->name}}</td>
                                    <td>{{$express->description}}</td>
                                    <td>{{$express->shipping_money}} / {{$express->shipping_free}}</td>
                                    <td>
                                        {!! is_something($express,'is_enable') !!}
                                    </td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$express->id}}">
                                        <input type="text" class="sort_order" name="sort_order[]"
                                               value="{{$express->sort_order}}" style="width: 80px;">
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('shop.express.edit',$express->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary">
                                                    <span class="am-icon-edit"></span> 编辑
                                                </a>
                                                <button class="destroy am-btn am-btn-default am-btn-xs am-text-secondary">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$expresses->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">

                                    {{$expresses->links()}}

                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>
    </div>
@endsection

@section('js')
    <script src="/vendor/daterangepicker/moment.js"></script>
    <script src="/vendor/daterangepicker/daterangepicker.js"></script>
    <script src="/vendor/moment/locale/zh-cn.js"></script>
    <script src="/js/daterange_config.js"></script>

    <script>
        $(function () {
            //单个删除
            $('.destroy').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);

                $.ajax({
                    type: 'DELETE',
                    url: '/admin/shop/express/' + id,
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                });
                return false;
            });

            //上面选中，下面也选中
            $('.check_all').click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            });
            //多选删除
            $('.delete_all').click(function () {
                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('您至少选中一条！');
                    return false;
                }

                $.ajax({
                    type: 'DELETE',
                    url: '{{route('shop.express.delete_all')}}',
                    data: $('.checked_id:checked').serialize(),
                    success: function () {
                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                });
                return false;
            });

            //排序
            $('.sort_order').change(function () {
                var id = $(this).parents('tr').data('id');
                var sort_order = $(this).val();

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.express.sort_order')}}',
                    data: {id: id, sort_order: sort_order},
                    success: function () {
                        window.location.reload();
                    }
                })
            });

            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);

                $.ajax({
                    type:'PATCH',
                    url:'{{route('shop.express.change_attr')}}',
                    data:{id:id,attr:attr},
                    success:function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                })
                return false;
            })

        })
    </script>
@endsection